<?php

/**
 * task actions.
 *
 * @package    mysmartmanager
 * @subpackage task
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class taskActions extends sfActions
{  
  public function executeSearch(sfWebRequest $request)
  {
    //$this->milestones = Doctrine::getTable('Task')->getResourceCostPerMilestone($this->getUser()->getAttribute('selectedProject')->getProjectid());
	$this->filter = new TaskFormFilter();
    
	if($request->isMethod('post')) {
     $this->filter->bind($request->getParameter($this->filter->getName()));
    }
	 
    $query = $this->filter->buildQuery($this->filter->getValues());
	$ids = array ();
	
	$result = Doctrine_Query::create()->select('m.milestoneid')
            ->from('Milestone m')
            ->where('m.projectid = ?', $this->getUser()->getAttribute('selectedProject')->getProjectid() )
            ->execute();
			
	 //$result = $result->fetchArray();
	 foreach ($result as $row):
	   $ids[]=$row['milestoneid'];
	 endforeach;
	//echo $result[0]['milestoneid'];
	$query->andwherein('r.milestoneid', $ids);
	/*
	if (strpos($query,'WHERE') > 0)
	  $query .= ' AND ';
	else
	  $query .= ' WHERE ';
	$query .= 'r.milestoneid in (select m.milestoneid from milestone m where m.projectid = '.$this->getUser()->getAttribute('selectedProject')->getProjectid().')';
	*/
	//echo $query;
	
	//$this->tasks = $query->execute();	
	$this->pager = new sfDoctrinePager('Task', sfConfig::get('app_max_tasks_to_show_in_milestone'));
    $this->pager->setQuery($query);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
  }
  public function executeViewcostpermilestone(sfWebRequest $request)
  {
   if($this->getUser()->getAttribute('selectedProject') == NULL) {
   		 throw new sfException("Please create some project.");
	} else {
    $this->milestones = Doctrine::getTable('Task')->getResourceCostPerMilestone($this->getUser()->getAttribute('selectedProject')->getProjectid());
	}
  }
  public function executeViewcostperday(sfWebRequest $request)
  {
   if($this->getUser()->getAttribute('selectedProject') == NULL) {
   		 throw new sfException("Please create some project.");
   } else {
    $this->tasks = Doctrine::getTable('Task')->getResourceCostPerTask($this->getUser()->getAttribute('selectedProject')->getProjectid());
	}
  }
  public function executeIndex(sfWebRequest $request)
  {
    $resourceId = sfContext::getInstance()->getUser()->getResourceId();
	if (!$resourceId)
	  $this->redirect('resource/new');
	if (sfContext::getInstance()->getUser()->getAttribute('selectedProject') == NULL)
	{
	  sfContext::getInstance()->getUser()->setFlash('error', 'There are no Projects related to you.');
	  $this->redirect('resource/show/'.$resourceId);
	}
	$this->milestones = Doctrine::getTable('Milestone')->getWithTasks();	
	//$this->tasks = Doctrine::getTable('Task')->createQuery('a')->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    //$this->task = $this->getRoute()->getObject();
	$this->task = Doctrine::getTable('Task')->find(array($request->getParameter('taskid')));
	$this->getUser()->setAttribute('currentTaskId', $request->getParameter('taskid'));
	$this->form = new NoteForm();
    $this->forward404Unless($this->task);
  }  
  
  public function executeNew(sfWebRequest $request)
  {
    
	$this->form = new TaskForm();
	$selectedProject = sfContext::getInstance()->getUser()->getAttribute('selectedProject');
    
	if($selectedProject == NULL) {
   		throw new sfException("Please create some project.");
	}
		
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new TaskForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($task = Doctrine::getTable('Task')->find(array($request->getParameter('taskid'))), sprintf('Object task does not exist (%s).', $request->getParameter('taskid')));
    $this->form = new TaskForm($task);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($task = Doctrine::getTable('Task')->find(array($request->getParameter('taskid'))), sprintf('Object task does not exist (%s).', $request->getParameter('taskid')));
    $this->form = new TaskForm($task);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($task = Doctrine::getTable('Task')->find(array($request->getParameter('taskid'))), sprintf('Object task does not exist (%s).', $request->getParameter('taskid')));
    $task->delete();

    $this->redirect('task/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
	
	$this->milestone = Doctrine::getTable('Milestone')->find(array($form->getValue('milestoneid')));
    if ($form->getValue('startdate') >= $this->milestone->getStartdate() && $form->getValue('estimatedenddate') <= $this->milestone->getEnddate()) {
	   $task = $form->save();
  
      if($form->getValue("taskid") == 0) {
		  $this->getUser()->setFlash('notice', 'Task created successfully.');
		} else {
		  $this->getUser()->setFlash('notice', 'Task updated successfully.');
		}
      $this->redirect('task/edit?taskid='.$task->getTaskid());
	  }
	  else
	  {
	    $this->getUser()->setFlash('error', 'Task dates cannot cross Milestone dates from '.date("d-m-Y",strtotime($this->milestone->getStartdate())).' to '.date("d-m-Y",strtotime($this->milestone->getEnddate())), false);

	  }	  
      
    }
  }
}
